import request from "@/utils/request";
import { jsonToQueryString } from "@/utils/common";
import type {
  SpuDataPageQueryResp,
  SpuDataPageQueryReq,
  SpuImageListResp,
  spuSaleAttrListResp,
  baseSaleAttrListResp,
  SpuData,
} from "./type";
import { BasePageReq } from "@/api/baseType";
enum API {
  //spu分页查询
  SPU_PAGE_QUERY = "/admin/product",
  //获取spu下所有图片
  SPU_IMAGE_LIST = "/admin/product/spuImageList",
  //获取某一个已有spu的销售属性
  SPU_SALE_ATTR_LIST = "/admin/product/spuSaleAttrList",
  //获取所有的基础销售属性
  BASE_SALE_ATTR_LIST = "/admin/product/baseSaleAttrList",
  //保存spu
  SAVE_SPU = "/admin/product/saveSpuInfo",
  //修改spu
  UPDATE_SPU = "/admin/product/updateSpuInfo",
  //删除spu
  DELETE_SPU = "/admin/product/deleteSpu",
}

export const getSpuPageList = (
  data: BasePageReq,
  query: SpuDataPageQueryReq,
) => {
  return request.get<any, SpuDataPageQueryResp>(
    API.SPU_PAGE_QUERY + jsonToQueryString(data, query),
  );
};

export const getSpuImageList = (spuId: number | undefined) => {
  return request.get<any, SpuImageListResp>(API.SPU_IMAGE_LIST + "/" + spuId);
};
export const getSpuSaleAttrList = (spuId: number | undefined) => {
  return request.get<any, spuSaleAttrListResp>(
    API.SPU_SALE_ATTR_LIST + "/" + spuId,
  );
};
export const getBaseSaleAttrList = () => {
  return request.get<any, baseSaleAttrListResp>(API.BASE_SALE_ATTR_LIST);
};
export const saveOrUpdateSpu = (data: SpuData) => {
  if (!data.id) {
    return request.post<any, any>(API.SAVE_SPU, data);
  }
  return request.put<any, any>(API.UPDATE_SPU, data);
};
export const deleteSpu = (spuId: number | undefined) => {
  return request.delete<any, any>(API.DELETE_SPU + "/" + spuId);
};
